package controllers.gcm;

import java.util.ArrayList;
import java.util.List;

import play.Play;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;

public class GCMSender {

    private static final String SENDER_ID = Play.configuration.getProperty("gcm.browserkey");

    private Sender sender;
    private Message message;

    public GCMSender() {
        sender = new Sender(SENDER_ID);
    }

    public void sendMessage(ArrayList<String> mobileIds, String text) {
        message = new Message.Builder().collapseKey("collapseKey").timeToLive(30).delayWhileIdle(true)
                .addData("message", text).build();
        try {
            // use this for multicast messages. The second parameter
            // of sender.send() will need to be an array of register ids.
            MulticastResult result = sender.send(message, mobileIds, 1);

            if (result.getResults() != null) {
                int canonicalRegId = result.getCanonicalIds();
                if (canonicalRegId != 0) {

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
